/*
 * snmp_packet2.cpp
 *
 *  Created on: 2016-10-31
 *      Author: root
 */

#include"snmp_packet.h"
#include"snmp_oid.h"
#include"PublicTool.h"
#include"PublicLog.h"
#include<stdlib.h>
static const SnmpOidInfo OID_sysDescr={"sysDescr.0",{1,3,6,1,2,1,1,1,0},9};
static const SnmpOidInfo OID_sysObjectID={"sysObjectID.0",{1,3,6,1,2,1,1,2,0},9};
static const SnmpOidInfo OID_sysUpTimeInstance={"sysUpTimeInstance.0",{1,3,6,1,2,1,1,3,0},9};//返回包大小
static const SnmpOidInfo OID_sysContact={"sysContact.0",{1,3,6,1,2,1,1,4,0},9};
static const SnmpOidInfo OID_sysName={"sysName.0",{1,3,6,1,2,1,1,5,0},9};
static const SnmpOidInfo OID_sysLocation={"sysLocation.0",{1,3,6,1,2,1,1,6,0},9};
static const SnmpOidInfo OID_sysServices={"sysServices.0",{1,3,6,1,2,1,1,7,0},9};
static const SnmpOidInfo OID_ifNumber={"ifNumber.0",{1,3,6,1,2,1,2,1,0},9};
static const SnmpOidInfo OID_ipForwarding={"ipForwarding.0",{1,3,6,1,2,1,4,1,0},9};
static const SnmpOidInfo OID_ipv6Forwarding={"ipv6Forwarding.0",{1,3,6,1,2,1,55,1,1,0},10};
static const SnmpOidInfo OID_ipCidrRouteNumber={"ipCidrRouteNumber.0",{1,3,6,1,2,1,4,24,3,0},10};
static const SnmpOidInfo OID_inetCidrRouteNumber={"inetCidrRouteNumber.0",{1,3,6,1,2,1,4,24,6,0},10};
static const SnmpOidInfo OID_ipv6RouteNumber={"ipv6RouteNumber.0",{1,3,6,1,2,1,55,1,9,0},10};
#if USE_SYS_IFINDEX
static const SnmpOidInfo OID_ipAdEntIfIndex={"ipAdEntIfIndex",{1,3,6,1,2,1,4,20,1,2},10};
#endif
static const SnmpOidInfo OID_RuijieSoftVersion={"RuijieSoftVersion.0",{1,3,6,1,4,1,4881,1,1,10,2,1,1,2,0},15};

int MakeSnmpSysPacket(char*PacketData,const char*communtiy,int request_id,const char*IP){
    int length=0;
    SnmpPacket*packet=CreateSnmpPacket(SNMP_VERSION_2c,communtiy,SNMP_MSG_GET,request_id);
    AddSnmpRequestOid(packet,OID_sysDescr);
    AddSnmpRequestOid(packet,OID_sysObjectID);
    AddSnmpRequestOid(packet,OID_sysUpTimeInstance);
    AddSnmpRequestOid(packet,OID_sysContact);
	AddSnmpRequestOid(packet,OID_sysName);
	AddSnmpRequestOid(packet,OID_sysLocation);
    AddSnmpRequestOid(packet,OID_sysServices);
    AddSnmpRequestOid(packet,OID_ifNumber);
    AddSnmpRequestOid(packet,OID_ipForwarding);
	AddSnmpRequestOid(packet,OID_ipv6Forwarding);
	AddSnmpRequestOid(packet,OID_ipCidrRouteNumber);
	AddSnmpRequestOid(packet,OID_ipv6RouteNumber);
	AddSnmpRequestOid(packet,OID_inetCidrRouteNumber);
#if USE_SYS_IFINDEX
    SnmpOidInfo OID_ipAdEntIfIndex_Tmp=OidNameAddIP(OID_ipAdEntIfIndex,IP);
    AddSnmpRequestOid(packet,OID_ipAdEntIfIndex_Tmp.oidName,OID_ipAdEntIfIndex_Tmp.oidLen);
#endif
    AddSnmpRequestOid(packet,OID_RuijieSoftVersion);
    //ShowSnmpPacket(packet);
    length=WriteSnmpPacket(packet,PacketData,0);
    FreeSnmpPacket(packet);
    return length;
}
void ParseSysDescr(const char*sysDescr,int ManufacturerID,char*softVersion,char*ProductName,char*Manufacturer);
//正确返回0，错误返回错误ID，非法返回-1
int ReadSnmpSysPacket(const char*PacketData,int DataSize,SNMPSYSPACKET*Info){
    SnmpPacket*packet=ReadSnmpPacket(PacketData,DataSize);
	if(packet==NULL)return -1;
    //ShowSnmpPacket(packet);
    Info->request_id=packet->request_id;
    int ManufacturerID=0;
    SnmpDataInfo*Pos=packet->data;
    while(Pos!=NULL){
		if(OidNameCmp(Pos,OID_sysDescr)==0){
			strcpy(Info->sysDescr,Pos->data);
		}else
		if(OidNameCmp(Pos,OID_sysObjectID)==0){
			strcpy(Info->sysObjectID,Pos->data);
			ManufacturerID=atol(Info->sysObjectID+strlen(".1.3.6.1.4.1."));;
		}else
		if(OidNameCmp(Pos,OID_sysContact)==0){
			strcpy(Info->sysContact,Pos->data);
		}else
		if(OidNameCmp(Pos,OID_sysName)==0){
			strcpy(Info->sysName,Pos->data);
		}else
		if(OidNameCmp(Pos,OID_sysLocation)==0){
			strcpy(Info->sysLocation,Pos->data);
		}else
		if(OidNameCmp(Pos,OID_sysUpTimeInstance)==0){
			Info->sysUpTimeInstance=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_sysServices)==0){
			Info->sysServices=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_ifNumber)==0){
			Info->ifNumber=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_ipForwarding)==0){
			Info->ipForwarding=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_ipv6Forwarding)==0){
			Info->ipv6Forwarding=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_ipCidrRouteNumber)==0){
			Info->ipCidrRouteNumber=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_inetCidrRouteNumber)==0){
			Info->inetCidrRouteNumber=Pos->value.valu32;
		}else
		if(OidNameCmp(Pos,OID_ipv6RouteNumber)==0){
			Info->ipv6RouteNumber=Pos->value.valu32;
		}else
#if USE_SYS_IFINDEX			
		if(OidNameNCmp(Pos,OID_ipAdEntIfIndex)==0){
			Info->ifIndex=Pos->value.valu32;
		}else
#endif			
		if(OidNameCmp(Pos,OID_RuijieSoftVersion)==0){
			if(Pos->valType==ASN_OCTET_STR)
				strcpy(Info->softVersion,Pos->data);
		}
		Pos=Pos->Next;
	}
    ParseSysDescr(Info->sysDescr,ManufacturerID,Info->softVersion,Info->ProductName,Info->Manufacturer);
    DEBUG("sysDescr:%s\nID:%d 厂商:%s\n软件:%s 产品:%s",Info->sysDescr,ManufacturerID,Info->Manufacturer,
          Info->softVersion,Info->ProductName);
    FreeSnmpPacket(packet);
    return 0;
}

